/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __511C45B1_8CD9_4d7b_8928_785D5DB0CB8E__
#define __511C45B1_8CD9_4d7b_8928_785D5DB0CB8E__

#include "../tools/head.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_common
{

    class md5_impl : public xos_stl::mem_item< xos::release_impl< md5_impl >, thread_lock >,
		public i_md5
    {
    public:
        typedef xos::release_impl< md5_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //

        static int get_xos_item_data( ITEM_DATA*& item_ptr );

    public:
        md5_impl();
        ~md5_impl();

    public:
        int init();
        int term();

    public:
        // i_md5 method
        int cal_update( const char * cal_buf, unsigned int uLen );
        int get_result( unsigned char md5_result[16] );
        int get_result_string( char * lpszMd5, int nLen );
        int get_hash2byte( int * pnHash2byte );
		int clear();

    private:
        unsigned int state[4]; // state (ABCD)
        unsigned int count[2]; // number of bits, modulo 2^64 (lsb first)
        unsigned char buffer[64];   // input buffer
        unsigned char PADDING[64];  // What?

    private:
        void MD5Init();
        void MD5Transform( unsigned int state[4], const unsigned char block[64] );
        void MD5_memcpy( unsigned char * output, unsigned char * input,unsigned int len );
        void Encode( unsigned char * output, unsigned int * input,unsigned int len );
        void Decode( unsigned int * output, const unsigned char * input, unsigned int len );
        void MD5_memset( unsigned char * output,int value,unsigned int len );

    };

} // xos_common

#endif //__511C45B1_8CD9_4d7b_8928_785D5DB0CB8E__
